* This code replicates the main results in Figures  of 
* Product Innovation and Credit Market Disruptions (forthcoming RFS)
* by Joao Granja and Sara Moreira
*
* The main data set is mainsample_placebo.dta
* This is a pseudo data set that illustrates the format of the original data set


clear all
set more off

use "mainsample_placebo.dta", clear
tsset firm year

********************************************************************************
* Figure 2: Evolution of Small Business Lending (2007--2010)
* Panel A : Evolution of Small Business Loans by County

use "$data/cleaned files/sbl_all", clear
destring year stcntybr, replace force
drop if year <2005
gen totalamount = amountloans_lower100k + amountloans_btwn100k250k + amountloans_greater250k
replace totalamount = totalamount/1000000
preserve
collapse (sum) totalamount, by(year)
tsset year

gen c=350 if year>2006 & year<2011

twoway ///
(area c year, bcolor(gs15)) 			///
(tsline totalamount ,  m(circle_hollow) mcolor(gs1) lcolor(green) lp(longdash_dot)) ///
	, xlabel(2005(1)2016, val angle(60) labsize(vsmall)) ///
	title("", size(small)) graphregion(color(white)) bgcolor(white) ylab(150(50)350, nogrid labsize(vsmall))	/// ///
	xtitle("Year", size(small)) ytitle("Amount Small Business Loans (in \$ billions)", size(small))	legend(off)			
	graph export "writeup/volume.pdf", replace
restore

* Panel B: Evolution of Small Business Loans by County

use "$data/cleaned files/sbl_all", clear
destring year stcntybr, replace force
keep if year ==2007 | year ==2010
gen totalamount = amountloans_lower100k + amountloans_btwn100k250k + amountloans_greater250k
collapse (sum) totalamount, by(stcntybr year)

foreach num of numlist 2007 2010{
gen totalamount`num' = totalamount if year ==`num'
egen total`num' = max(totalamount`num') ,by(stcntybr)
}


gen ratio = (total2010/total2007 -1) * 100
drop if ratio>100
histogram ratio, bin(10) ///
    xlabel(-100(10)100, labsize(vsmall)) ylab(, nogrid labsize(small)) ///
    title("", size(small))         ///
    fi(inten70) fcolor(maroon) lcolor(maroon)    bsty(none)        ///
    xtitle("% Change in Small Business Lending (2007-2010)", size(vsmall))                yline(0, lcolor(gs5)) legend(off)                                ///
    ytitle("Density", size(small)) ///
    xscale(noex lw(medthick)) ///
    text(1100 .1 "", place(se) si(vsmall) orientation(vertical)) graphregion(color(white)) bgcolor(white) 
 graph export "writeup/histogram.pdf", replace


********************************************************************************
* Figure 3: Product Innovation and Credit-Market Disruption: Entry Rate
		
use "$data/work files/sample_bartik.dta", clear
tsset firm year

fastxtile shock_terciles = bartik, nq(4)
preserve
drop if year==2006
winsor2 entryrate entryrate_a entryrate_b, replace cuts(0 99.5)
gcollapse entryrate entryrate_a entryrate_b, by(shock_terciles year)
tsset shock_terciles year

gen entry_2007 = entryrate if year==2007
egen entry2007 = max(entry_2007), by(shock_terciles)
gen index = entryrate/entry2007
 
twoway ///
(tsline index if shock==1,  m(circle) mcolor(gs1) lw(thick) lcolor(red) lp(longdash)) ///
(tsline index if shock==4,  m(tringle) mcolor(gs1) lw(thick) lcolor(green) lp(shortdash_dot)) ///
	, xlabel(2007(1)2015, val  labsize(small)) ///
	title("Product Introduction Rate (All)", size(medlarge)) graphregion(color(white)) bgcolor(white)	/// ///
	xtitle("", size(small)) ytitle("# New Products/Total # Products", size(small))	///					///
	legend(label(1 "High Local Credit Shock")  label(2 "Low Local Credit Shock")  size(small) pos(4) ring(0) col(1) region(lwidth(none))) 
	graph export  "writeup/entryrate.pdf", replace
restore


preserve
drop if year==2006
winsor2 entryrate entryrate_a entryrate_b, replace cuts(0 99.5)
gcollapse entryrate entryrate_a entryrate_b, by(shock_terciles year)
tsset shock_terciles year

gen entry_2007 = entryrate_a if year==2007
egen entry2007 = max(entry_2007), by(shock_terciles)
gen index = entryrate_a/entry2007
 
twoway ///
(tsline index if shock==1,  m(circle) mcolor(gs1) lw(thick) lcolor(red) lp(longdash)) ///
(tsline index if shock==4,  m(tringle) mcolor(gs1) lw(thick) lcolor(green) lp(shortdash_dot)) ///
	, xlabel(2007(1)2015, val labsize(small)) ylabel(.5(.25)1,) ///
	title("Product Introduction Rate (Existing Product Lines)", size(medlarge)) graphregion(color(white)) bgcolor(white)	/// ///
	xtitle("", size(small)) ytitle("# New Products in Existing Modules/Total # Products", size(small))	///					///
	legend(label(1 "High Local Credit Shock")  label(2 "Low Local Credit Shock")  size(small) pos(4) ring(0) col(1) region(lwidth(none))) 
	graph export  "writeup/entryrate_a.pdf", replace
restore

preserve
drop if year==2006
winsor2 entryrate entryrate_a entryrate_b, replace cuts(0 99.5)
gcollapse entryrate entryrate_a entryrate_b, by(shock_terciles year)
tsset shock_terciles year

gen entry_2007 = entryrate_b if year==2007
egen entry2007 = max(entry_2007), by(shock_terciles)
gen index = entryrate_b/entry2007
 
twoway ///
(tsline index if shock==1,  m(circle) mcolor(gs1) lw(thick) lcolor(red) lp(longdash)) ///
(tsline index if shock==4,  m(tringle) mcolor(gs1) lw(thick) lcolor(green) lp(shortdash_dot)) ///
	, xlabel(2007(1)2015, val labsize(small)) ylabel(.5(.25)1,) ///
	title("Product Introduction Rate (New Product Lines)", size(medlarge)) graphregion(color(white)) bgcolor(white)	/// ///
	xtitle("", size(small)) ytitle("# New Products in New Modules/Total # Products", size(small))	///					///
	legend(label(1 "High Local Credit Shock")  label(2 "Low Local Credit Shock")  size(small) pos(4) ring(0) col(1) region(lwidth(none))) 
	graph export  "writeup/entryrate_b.pdf", replace
restore
